package com.cbhb.sys;
import java.util.*;

public class MoreThanHalfNum {

    public int moreThanHalfNum(int[] array) {
        int size = array.length;
        Map<Integer, Integer> map = new HashMap<>();
        for(int i=0;i<size;i++) {
            int item = array[i];
            int value = map.getOrDefault(item,0);
            map.put(item, ++value);
            if(value > size/2) return item;
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(new MoreThanHalfNum().moreThanHalfNum(new int[]{1, 2, 3, 2, 2, 2, 5, 4, 2}));
    }

}
